{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 入门\n",
    "\n",
    "### 什么是符号计算?\n",
    "\n",
    "符号计算象征性地处理数学对象的计算。 这意味着数学对象可以精确地而不是近似地表示，带有未赋值变量的数学表达式以符号形式保留。\n",
    "\n",
    "下面将以一个示例来表述通过Python内建的函数来计算开平方。以数学的方式计算如下: "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.0"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import math\n",
    "math.sqrt(9)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "9是一个完全平方数，所以我们可以获得精确的解 3。但是在对非完全平方数做开方运算时会获得一个不精确的数值。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2.8284271247461903"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import math\n",
    "math.sqrt(8)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这里获得的是一个近似解。2.82842712475 并不是8精确的平方根（实际上8的平方根不能被描述为一个有理数）。如果我们仅仅关心十进制的8的平方根，看似可以解决。\n",
    "\n",
    "但是如果需要做到更好，例如 sqrt(8) = 2*sqrt(2)。从上面的结果中推断出来就非常的困难。这就是符号运算的用武之地。对于像SymPy这样的符号计算系统，默认情况下，对不为完全平方数数字平方根不进行求值。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "sqrt(3)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import sympy\n",
    "sympy.sqrt(3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "此外，这里我们可以看到符号计算的真正功能，符号化的结果可以简化。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2*sqrt(2)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import sympy\n",
    "sympy.sqrt(8)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 一个更有趣示例\n",
    "\n",
    "下面的例子开始展示我们如何精确的操作无理数数字通过SymPy. 诸如SymPy之类的符号计算系统(通常也称之为计算机代数系统，或简称CAS)能够计算带变量的符号表达式。\n",
    "\n",
    "下面我们将看到在SymPy, 变量被定义为符号。不像许多符号操作系统，变量在SymPy必须在他们使用前被定义（具体的原因会在下一节介绍)。让我们定义一个符号表达式，描述数学表达式 **x+2y**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "x + 2*y"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sympy import symbols\n",
    "x, y = symbols('x y')\n",
    "expr = x + 2*y\n",
    "expr"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "注意我们所写的x + 2*y 就像x和y时普通的变量一样。但在这种情况下，表达式不求值，而是保持为x + 2*y。现在让我们来改进一下: "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "x + 2*y + 1"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "expr + 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2*y"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "expr - x"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "注意在下面的示例中，当要求计算`expr -x`，我们不会获得`x + 2*y -x`，而是`2*y`。x和-x将会被化简。这类似于`sqrt(8)`会被自动的转换为`2*sqrt(2)`。不止如此还可以: "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "x*(x + 2*y)"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x * expr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x - x"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这里我们也许期望`x(x + 2y)`转换为`x**2 + 2*x*y`，但实际我们只获得左边的部分。化简不会被自动触发，因为在不同的场景下可能有不同的用途。在SymPy提供了这些转换的方法。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "x**2 + 2*x*y"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sympy import expand, factor\n",
    "expanded_expr = expand(x*expr)\n",
    "expanded_expr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "x*(x + 2*y)"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "factor(expanded_expr)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 符号计算的威力\n",
    "\n",
    "真正符号计算系统类似SymPy是能够进行各种符号计算的。SymPy可以化简表达式，计算导数，积分和极限，求解方程式，使用矩阵，等等，而且都可以象征性的完成。它包括绘图，打印（如数学公式的2D漂亮打印输出或LATEX)，代码生成，物理，统计，组合，数论，几何，逻辑等模块。这些是也仍然是SymPy抽象计算能力的一小部分。\n",
    "\n",
    "下面我们会进行一个关于漂亮打印的完整功能的展现: "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAALsAAAAWCAYAAACYEu1aAAAABHNCSVQICAgIfAhkiAAABdpJREFUeJztmmtsVEUUx39tAdugKSo+g7JGaACJ+EIlwQSVVCVGE1GUbytEYlCMGjWAon4RFUUwahBBAyYGihBNSEQxSvGBCK02YjVKIC0Ky0vAEC2F1vXDOZPe3s597O69u21yf1/u3pkzZ2bnP3fm3LMLCQkJobgHaAeGOspeB3YB5+XoKwVkgRVRDEx5HzgIDCzAx9XIuKZHMqLiEKUuxaRX61UGNADL9P4JZLDD8/CVItrFfg3wH/B4BL4+AjLA6RH4KgZR6lIs+oRetcApYDZwHBibp5/+wAjggojGtRE4BlRF4Ota5EGcG4GvYhGVLsWiz+i1BegAbovDeR7UILvEOxH6/BVoBSrybJ9GBJgQ0XjC0Nt08aIoepV7GE4GNgCHgZPATuQpsQl9EzAGOToPePi7A/gCOVragX3AZmCmwyaFPYxxlqeA1TquE8hRfbulv2k6njqP8WxUn3e5ysu0nyzwkqtuNXAxMNHDZzGIWheQXbAO2Itok0HmZ4rFdgrwFfA30AbsAOYAp1lsw2huKIleFcAqbbwTWAIsBn7TspUu+zHI0TMNWAd8ZvE5Q9tmkCd3PrAc2AZsd9il8F/sm5C4cyuwSMdyAugEbnS1aUB2NK8XnTHa7he6L5SF2pdth5moda96+AwiTf47exy6ADyAzFM78CFd2jQB9S7b+drXIe3/FeBnLatHwlBDWM0NJdHrTTV4EejnKO8PfKt1o7RsKLIbPKv3o5GjaILLZyMymeda+hvs+JzCf7Fngedcdbdo+SeOsoHIxO2w9OdkhbZN6/1cva/DfuJVa/22AL9epMl/scehyygkpj8CXGbpc4jj8z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qRLMH6Av1UIbvd2oJHlOBH7Hrl+w++vkAH8APyJ6UG5PjPh+FITLsA71x/QuEkeiUkJCQkJCQk2PkfRfs/Gcnn3NYAAAAASUVORK5CYII=",
      "text/latex": [
       "$$e^{x} \\sin{\\left (x \\right )} + e^{x} \\cos{\\left (x \\right )}$$"
      ],
      "text/plain": [
       " x           x       \n",
       "ℯ ⋅sin(x) + ℯ ⋅cos(x)"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sympy import *\n",
    "x, t, z, nu = symbols('x t z nu')\n",
    "init_printing(use_unicode=True)\n",
    "diff(sin(x)*exp(x), x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'e^{x} \\\\sin{\\\\left (x \\\\right )} + e^{x} \\\\cos{\\\\left (x \\\\right )}'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "latex(diff(sin(x)*exp(x), x))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAE8AAAAWCAYAAACBtcG5AAAABHNCSVQICAgIfAhkiAAAA89JREFUWIXt12mIVmUUB/DfaGOKhe1RVL6QioZkkbaAQYtYSfRBiehLlEFE5ReJMKHlU0UkFgViC1gQaigVQctE2ESLuVQkFiFGto2ZjULkksvbh/NcvHO7933H8UYo84fhznPOef7Puec95zznMoj/DTdjL0bnZM9gM848TK4GmlhSh2MJr2AbRh4BxyXCrztr8SiHDqzDC2l9v3B27AC4GuoN3mQcxNwauF5HD06ogasPpmMf5uFPTBkgTyfG46ya/OrCToyogetS8cPOr4HrX/gU+3HDf0E+AIwTWfd8jZzfYguGZoIhFYaz8A62429sElEfWmJ7DSaJEv6tgu8mfCBSfy9+RTfuydk0lJdtXt7AsuTXHtEybiw5b3byZ3mFP12Jc2ZB3pHOaeKJgm4ZzsO0Ck5DsTRt3oRFeBrfJdnLBftJojRmYyXeK+G8K+3tEZnwGF7EGqzN2TW0Dt4q0U9XY2HyZQ8O4OrCnnWiEqouiklp3zf6JsSCdFZZxk5LuqcqOD2XDB7HcTl5Jz5JuguSbDR+wcNpPVGUylUFzvUi284oOe+03P8NrYPXxCMF3XVJ/nZONlIEbkPJeXksSXtvT+v5ab1ceUWOSvo1ZWSXiZd/o+KwLIPuwCmiBywu2CzHZwXZevyFk6veIqGhdfB+UN42togyzjAu2Xe1Oe8c7E6896U972JYiz27sTVb5LNrjqj5XXi0ZOPE9OxALyaU2NxSIntVlMNGEdxukcW/t3CyDF+JUiviJ1yRW5+anjva8P0sWtI8PCsuvZmix1ehV8X8us2h8mj1V9kwW+A20asOJI6DoodNztk0tL8wyvBh0me4KK3f7Idfcx16r/H9sO8V41gfDE8E3f0gOBKchBliqD6APxzqhQ31BO/stP64jS+3ih+xJ9kvamM/JNlvLipGJIKNbQjqxEvpzFlp3VBP8DpEFbVqCzNEeW7A6aJ/79M6+yakc1ZmguxW2Y2vxU1anH0yTFXesNvhen17a4Ys43YNgLMVmvhI3ORjSvRTsUL0vOkiyA8lH4uzXR6Xp+eqMmX2mdXE+6LJL8RrIlV/PMyXyLBT3FArxIy0QFz3TTGPdSa7hnoyjyjJJu4tyLO5tAfnF3Rr054rK85ZKkagcyv0poiX3CoCuV2k9mJcW7WpDe4WH9bfiyzrxZd4ACfm7BrqC94w8Q6f52RjkmwHLizhyYbg1SW6UaI6q8a4Yw4PimBcXAPXHK2z8pjDcDFAv3WEPCPEt/iKomIgF8DRgv34AseL3rpvgDxjxeflk6JfDmIQgzh68Q/wBRgmOo4SbwAAAABJRU5ErkJggg==",
      "text/latex": [
       "$$e^{x} \\sin{\\left (x \\right )}$$"
      ],
      "text/plain": [
       " x       \n",
       "ℯ ⋅sin(x)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "integrate(exp(x)*sin(x) + exp(x)*cos(x), x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'e^{x} \\\\sin{\\\\left (x \\\\right )}'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "latex(integrate(exp(x)*sin(x) + exp(x)*cos(x), x))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAE8AAAAeCAYAAABt5kPUAAAABHNCSVQICAgIfAhkiAAAA3VJREFUaIHt2VuIVVUcx/HP6GSWkk1FikSUYAW+RNZbCVNglzErCuxC9qCYFXTPIAgsQoOSIqOIKIvonkVQ2eUlsnoKrMykC9GNbkQmXexil4e1jm327H322vvMmWnkfGGYvf97/dfv/197Xf7nHHr0GAv6Gvj8M8b645a5WD7WQfxfmFCz/RCe70Yg45G6g3cwvuhGIOOROoN3IL7pViDjkTqDdwpe6FYguzt3q7/MO+Uk/I7nhFN+pP5GlT1w52iL4hFcPAa6I8ogzi6w74+leAYfYwe243Us0dlM3QfbsF8HfVQxHX/hjnjflXxuxkCBfbmwDL7Cw1iN+/FjtD+leSG8FOsb+qayTIhzMN53lM8E9BfY7ylpfzxONfyNzMDnUfDMhCSK2IiFDX1T2YDvMTHeN85nAC/igpx9FlY0COy6KLa2ge8sfCfstWUMST8gjizwnyYcRusSYxqWT3aWzcG3WIAHM/YFmn2q+DP+39mmzRRhb/s6Z1+MRzN9FLFdqAAuwhZhSbU4AouEveoVvFvgP4RJeLqNRpbKfGbHoLJvvGzJtqMfm4U3dWLB88OEwflF2Fuy9OETHJWgc0bUuClnXxHtS9r4PomfMTlBpyqfXXyAE+L1FKxJ6DzPrVGobMbOwNF4TRjAqZln8/Beos4NUeesnP2xaJ9b4jcZPwkDmEJVPrtYg9vi9Wk4OVGgxaVRaKvqMqN12i3O2O7DNYlaz0b/2Tn7h8Iy27PEb2H0OzdBo04+BvFRvL5d2rRucUkU2iLMrir2xW94Od7vJdR2MxP1PhNmULZ8mIq/hWVWxjrhsJhW0X/dfPQLdc3huCvFIXJ5FNosfImQynqhUJ2J84QTP4WBqPdGzn5ctD9U4jdRKE82VPTfNB+PR/Flie2vjUKbcEAdIf9t+lfhJZyT6DcY/fIv+LJMf+382uXWST7OF6b+QQltr49Cb2n2UWoSfhBO2G3C0k3hCsWDcG+0zy/xWyvM9Oklz5PzKfo0QfjqaRO+bOcsFNQ3xmA2Cptrnk/xQJs+/sATuFA4LHZUaLZoFb7v5OythOfgfcNzOB1vCjVtnpHIBxxS1QArVVf3ryb0c2xsOy+hbYu3hST3ztkXCXvar7g69+yYqHNlSZ8rjUw+o0ofbtH9X9NWCQNwaJd1dku2CjO2R48ePXrU5F/djgBt3j/j3wAAAABJRU5ErkJggg==",
      "text/latex": [
       "$$\\frac{\\sqrt{2} \\sqrt{\\pi}}{2}$$"
      ],
      "text/plain": [
       "√2⋅√π\n",
       "─────\n",
       "  2  "
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "integrate(sin(x**2), (x, -oo, oo))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAA0AAAASCAYAAACAa1QyAAAABHNCSVQICAgIfAhkiAAAAHZJREFUKJFjYKACCGFgYJjMwMBwmIGB4RMDA8N/BgaGJYQ0XYAq/MzAwHCdWE2ODAwMqgwMDIwMDAwOuDSxoPH3EzKVgYGBgYkYRaOaBlwTeuQGQDEDAwODBJS2ZGBgWABlv2FgYChBN6SBAZJ0cOEH5LiMzgAA6XoX52TB9a4AAAAASUVORK5CYII=",
      "text/latex": [
       "$$1$$"
      ],
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "limit(sin(x)/x, x, 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAHcAAAAeCAYAAAAWwoEYAAAABHNCSVQICAgIfAhkiAAAAtFJREFUaIHtmk+IDmEcxz+2XSmXXSQHaVNy4Ea5IKQUKwdHbZ4i0VtSDk5qc7FJchIXEYpSDtgopDg4qD0spVAb0jpZUg7+rMNvtNu878w8z8wzv+dh51PT7s77PDPfT7/ZZ5555oWGWcUu4AVwA7gOrA0bp8GCFlKre0jt5mY1NMAUME8lVjtTnreY8e1qmFG7LjUNO9YAh4A5HrdYqd01tuLuAO6GDqFE7a6xFXcZ8D50CCVqd42puIuBidAhlFBxjam424GR0CGUUHGNqbjrgGehQyih4hpLcXuAX8Dv0EEUUHONpbjrgacZny0E9gO3gDfAd+BL0n4f8TjYkuWq4mnQX8QYBvoyPjuI5PkIXANOAheByWT/TeJ+nk2T5erD01BQu8IGFegCujvsv5DTZwuwk/YrdwnwDsm620s6v7i6+vA0BFqh6kNmiHtS+5cDb3P6PQJu036PmgDOJ79v8pDPJ2VcvXtqFncV8AkYSO0foPxKzY/k58+yoWrCt6s3T0N9w/IKZJLQM2Nf3pCcRzcwhmTdVjFXHfhydfE0BHxx8BoZZjYmf88HvpU81jCwGhn+7leP5h1frqU9XYo7jtvrp6sZx7nD9HC1FXjgEjjhMHAUeAUMluivRVVX756GzsPyw+QkttupjONvRq5qgLMdzlNEK8n3EplJxkwV1zKehoCPQiD3kElgJXDOse8RJNsYsvgeO2Vdy3oaAhcX5Cs8V4ADDn2OIblGgUV1hKoJV9cqnoYIijuIPM8ttWx/HMn0HFjgeK5LTH8FJQQurlU8IVW7TisoGowgV+YHi7Z7gRPIYvsTZJKRZhwpYif+ThpDPQvbulb1tMKgs7bcb9luiOKZ+eOc/qPAV7LXrjXot2gzRDVPiGRY1qIX+U/Imrn/bxgKhuXPyPT7ctLwNHIP+BfZgCzdnQkdpGZaiGsvUrvZ8F68oaGhoaEhGv4AsUwQ0HnfTEQAAAAASUVORK5CYII=",
      "text/latex": [
       "$$\\left [ - \\sqrt{2}, \\quad \\sqrt{2}\\right ]$$"
      ],
      "text/plain": [
       "[-√2, √2]"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "solve(x**2 - 2, x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAPwAAAAaCAYAAACTtjH4AAAABHNCSVQICAgIfAhkiAAAB31JREFUeJztnGeMFVUUgD/KElaWoAEirohoFISooAL6Q5CiEAsRIRrFwrOEGCmhaBQUJEYEEyJVEE0QiKgRFiwxKhIFpalZJUixJayd5iIIrDTxx7mTN/uYW+a9mZ0F5ksms+/2e885d869d2YhJSUlJeWkYArwYdKNSKmVBOpG3QQakhIdnYEvk25ESq0kVt1YCOwEGjmmvwo4DjwYV4NOcYqAw8gYetfmhNoSVvYp1YnaFmLXjU7Af8AoTfwoVenAnPBlwJ9ASZSNKYCLgUlAObALOKLuK4BHgDOSa9oJ1CWrKF2AFsBZCbTDJnuPk2ls/eh01+Ns4BgwQ/1uCjyE6PZPQBWwF1iNGLTOo47SFmLXjeXA30CxJv41VXnbnPAuKnxslI3JgzrAM8AhpD1rgDnAZOAN4C8V/nlSDdRwC7APaX9S2GR/so6th053PQar+B7q98Pq9x/AImSSm4eM0XFgCcHyitoWYtONNsgM/7IhzVbgH4Jnt63Az0C9qBsWglfJuj0dAuKLgSdVujh5lupuWNDV3Zd+HIUZSiagzDC4yD7Jsc1QWP/ArLsAHwC7yepvT6BvQPoWwC+qPQMMdUVlC066MVo1aLQmvi0yU3/mC5us8vQKSP88esW9R6V5Wv3uY2tcTHgu2xagsSWt7ilmYwBZxTgM/IjM5LmCbQZcYrn8rm8ZWVc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ffPpLs9MG/mATyu0nyDHLe6EGQLketS0ICvRs575yLHRE9p8h5HZp5mwEUB8d7sv89Qv+dJBK2zcmmNKJrrlbGU5z0VZiNLl1zqIMceb1rKaYyMo7eerQI2Ik+J/po815Lsy0aFYpN9VGObFCbd7Y9shr2jyTsOeUehHJnwXI9ag2whcl3KdelB1vGDkSfXSMzrjjLkZYA+yPmgn3J1n4hsLBxAZir/zmZv5OmoG7w4mQFcDjyAbMwsB35ANmrOQwayJfC6pZzpyPJhnS/sMWR9V4a4+xuRSeFcZH1aBLSKqB9JYZJ9VGObFCbd7YcsfXcE5BuELPGOIW+6DQ9IU0Gw96mzhdh1qSvZ80DbLnID5BhBtxs7FDmi+VeVOdEX1wSZwXTr7JriJmS39XdkI6YSEe5biAB1exQALyDvTV8YENcZeXd6O6LouxHln4vd1a0JMhR2bGWTPRQ2tkm69BCsu53V37qPhSZgX2quDMhns4VYdeldZKbp7Jh+DNKRK0LWM0zl6xoyX21hKvKFU5DLerqQr+xdKNTg4+A5pL8XRFxuYrbgbdTNCpGnIfLS/3sh8hQjT8YlIfLUJqYjLl27pBuSMPnI3pXaaPBbsR/phaXGbaEV8ATwCnLmuInw3yd3Q17+d/0nCO0Q96d1yHpqAy8imzk9ka+gvKu2fNdf04SVvYkS5Py8I3KGP179fbLvd5iocVvwXs3bg6ytSs3JT3t0a7MJCbbpVKE7+R2xpqSkpKSkpKSkpKSkpKSkpJxS/A9LUH7Jaj7WpgAAAABJRU5ErkJggg==",
      "text/latex": [
       "$$y{\\left (t \\right )} = C_{2} e^{- t} + \\left(C_{1} + \\frac{t}{2}\\right) e^{t}$$"
      ],
      "text/plain": [
       "           -t   ⎛     t⎞  t\n",
       "y(t) = C₂⋅ℯ   + ⎜C₁ + ─⎟⋅ℯ \n",
       "                ⎝     2⎠   "
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y = Function('y')\n",
    "dsolve(Eq(y(t).diff(t, t) - y(t), exp(t)), y(t))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAUcAAAAjCAYAAAAHW8iTAAAABHNCSVQICAgIfAhkiAAAB2NJREFUeJztnXmIVVUcxz/qFGONaUZphGCSmTmmZdKCymirqOVUhpDljaIMoWzDJIspKC1Ks7BFskZa/ijKInOSSgNFDQwtyxRbBjWXssUW2tTpj999vOm9ue+ec+99d3nv94HHW87yfm/u9/7OPb/7O2dAURRFURRFUaKgBpgKXJ+0IRHRloKHkk1UN5XFW8DkoI1rgVXAne7rrDMMmJa0EUomUe1UHt2Bl4D5QRo/BiyO1JxkuR/ok7QRSiZR7VQmnYBPgYttGnUF/gAGe5RPBz4DfnUf64BxwW2MheeTNkDJLKodO7LkH+5FpthFdPZocD5wFPClR/kuYCZwFnA2sNL9gjNCmVk+TgD2Jm2EkklUO/ZkyT/sBhpsGkzHPgj8E3CzZZu4cBCHryi2OKh2oiCt/sFBfF3PwgKvK8fuFp13Qe761AFrbS2LiXOA9UkboWQS1U44suAfAI4p/KDGo+KRBp0NRmIJtcDvQCOwObBp5eMI4BBwOGlDlMyh2glOVvxDjiJf6HXlaMI2YChwLvAMsASoD9FfuRgBrClRfhXwFLAaCR63AS+XqO/gn5N2yMCuXm69J933xwE3AkuBr4A/gQOu7TcQ7lh1hO3vrkai1I5DZejGlLj8Q9l07HXlaMI/yMEA2AAMB25HDkiauAR4pET5bGAIMrrtAk7z6W8T8IBH2UhgDNBiYNfliHCXuu8nISLag+SX7kBOhCuQu6Vj3TpRJQTb/u5qJErtVIpuTInLP4TVsfWsoAn40bLNSsJ5bAc5gA0B23emY2f/nE+70UB/JOepgXAjzzq3/WUGdVuA/UhMBuTkmEDxSN8bEXwbcGVAuzoiyt+ddZLWTlp14xDunGxPWP/gRdhjcKnbpn9hgdcldy/gmxIdzkVGu75IbGGOa9grFkZFybHAcuCags/7AV/7tF0FbCf8yFqPT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pK5ooHRNYgRhdj2TAL0VSIYr2RLPAIbqlSn0t6jZROi7S6tFuoFu+k/xSrlIMd+vfEdCONiRZtRTNmF/Z+X1fawdtNiKL+60FlSH6WtRtwv5vmEbdeOEQ7JyMwj80Y6blJoKdvzkct94ppoblvtCUOmTkmuBXsYp5GPmbnpy0IQHpgRzjR5M2pMrIum4g3f7BwcM5esUcbadN3dy+frZsV000Iv/M59ukDQnISGT1yLykDakysq4byIZ/8E3hyXEbdleOryFTLpNpgqIo1UWa/YOD+LpuhQVeN2RyO/J0wt9JzkM2BR2B2WadiqJUD2n3DzXIQgHj5dI9kan1ST715iPLfHSjVEVRCsmCf5hHfpmkMa8i6zO9WICkFmQ1H1JRlPKRBf/QE8lvHGzb8HhkGdIUirPZFyIpHWOQ3KLcoy6MpYqiVARZ8A99kM1FAv+72B7AQ8iazfZ45RQ1Bf0iRVEqhiz4h8eBM5M2QlEURVEURVEURVEqlP8A+f3dQ1nhXPQAAAAASUVORK5CYII=",
      "text/latex": [
       "$$\\left \\{ \\frac{3}{2} + \\frac{\\sqrt{17}}{2} : 1, \\quad - \\frac{\\sqrt{17}}{2} + \\frac{3}{2} : 1\\right \\}$$"
      ],
      "text/plain": [
       "⎧3   √17       √17   3   ⎫\n",
       "⎨─ + ───: 1, - ─── + ─: 1⎬\n",
       "⎩2    2         2    2   ⎭"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Matrix([[1, 2], [2, 2]]).eigenvals()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAKEAAAAkCAYAAADhLFrdAAAABHNCSVQICAgIfAhkiAAABnBJREFUeJztm2uMFEUQgL89XoeHniAC4gsvgBA0AU4xEiB3PkAFUaMJKgFCJAgqoqIYMURUIhg1GB4+o4hGBQUPgoCCRhDwFRNARKIogoAoGJUo4AM9f1SvOzvMTPf0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- \\frac{1}{2}}\\left(z\\right)$$"
      ],
      "text/plain": [
       "√2⋅√z⋅jn(ν - 1/2, z)\n",
       "────────────────────\n",
       "         √π         "
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "besselj(nu, z).rewrite(jn)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\\\int_{0}^{\\\\pi} \\\\cos^{2}{\\\\left (x \\\\right )}\\\\, dx'"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "latex(Integral(cos(x)**2, (x, 0, pi)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 为什么使用SymPy?\n",
    "\n",
    "有许多计算机代数系统。这篇维基百科文章(https://en.wikipedia.org/wiki/List_of_computer_algebra_systems)列出了其中的许多内容。是什么让SymPy比其他选择更好？\n",
    "\n",
    "首先，SymPy是完全免费的。它是开源的，并获得了BSD的自由许可，因此您可以修改源代码，甚至可以出售。这与流行的商业系统（如Maple或Mathematica）形成鲜明对比，后者的许可费用为数百美元。\n",
    "\n",
    "其次，SymPy使用Python。大多数计算机代数系统发明了自己的语言。不是SymPy。 SymPy完全用Python编写，并且完全用Python执行。这意味着，如果您已经了解Python，那么使用SymPy就会更容易上手，因为您已经知道语法（如果您不了解Python，它真的很容易学习）。Python是一种经过精心设计，经过考验的语言。 SymPy开发人员对自己编写数学软件的能力充满信心，但编程语言设计则完全不同。通过重用现有的语言，我们可以专注于重要的事情：数学。\n",
    "\n",
    "Sage是另一个计算机代数系统，也使用Python作为其语言。但是Sage很大，下载量超过1 GB。 SymPy的优点是轻巧。除了相对较小外，它除了Python外没有其他依赖项，因此几乎可以在任何地方使用它。此外，Sage的目标和SymPy的目标是不同的。 Sage旨在成为一个功能齐全的数学系统，并通过将所有主要的开放源代码数学系统整合为一个系统来实现这一目标。当您在Sage中调用某些功能（例如集成）时，它将调用其中包含的一个开源软件包。实际上，SymPy包含在Sage中。另一方面，SymPy旨在成为一个独立的系统，其所有功能都在SymPy本身中实现。\n",
    "\n",
    "SymPy的最后一个重要功能是可以用作库。许多计算机代数系统专注于在交互式环境中使用，但是如果您希望自动化或扩展它们，则很难做到。使用SymPy，您可以轻松地在交互式Python环境中使用它，也可以将其导入自己的Python应用程序中。 SymPy还提供API，可轻松使用您自己的自定义函数对其进行扩展。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
